package com.orm.mybatis.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class CopyFolderUtil {

    private final static String regex = "((?!0000)[0-9]{4}((0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])(29|30)|(0[13578]|1[02])31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)0229)";


    public static void copy(String dest, File file ,boolean onlyDelFile) {
        if (!dest.endsWith("/")){
            dest = dest+"/";
        }
        if (file.isDirectory()){
            File[] files = file.listFiles();
            for (File file1:files){
                String path = dest+file.getName()+"/";
                copy(path,file1,onlyDelFile);
            }
        }else {
            File file1 = new File(dest);
            if (!file1.exists()){
                file1.mkdirs();}

            FileUtil.copy(file,new File(dest),true); //有hutool用这个

//            try {   //没hutool用这个
//                FileCopyUtils.copy(file, new File(dest+file.getName()));
//            } catch (IOException e) {
//                e.printStackTrace();
//                log.debug("报错");
//                return;
//            }

            if (onlyDelFile){
            file.delete();}

        }
    }

    public static List<File> copyAllDateFolderInFolderToList(File folder) {
        List<File> list = new ArrayList<>();
        if (folder == null) {
            log.error("为空");
            return null;
        }
        if (folder.isFile()){
//            list.add(folder);
            log.error("目录为文件");
            return list;
        }else if (folder.isDirectory()){
            addDateFolderToList(folder.getPath(),folder,list);
        }else {
            log.error("不存在此文件");
            return null;
        }
        return list;
    }
    private static void addDateFolderToList(String parent,File file,List<File> list){
        if (!parent.endsWith("/")){
            parent+="/";
        }
        Pattern p=Pattern.compile(regex);
        File[] files = file.listFiles();
        for (File file1:files){
            if (file1.isDirectory()){
                Matcher m=p.matcher(file1.getName());
                if(m.matches()&&!m.group().equals(new SimpleDateFormat("yyyyMMdd").format(DateTime.now()))){
                 list.add(file1);
                }
                String path = parent+file1.getName();
                addDateFolderToList(path, file1, list);
            }else {
//                list.add(file1);
            }
        }
    }

    public static List<File> copyAllFileInFolderToList(File folder) {
        List<File> list = new ArrayList<>();
        if (folder == null) {
            log.error("为空");
            return null;
        }
        if (folder.isFile()){
         list.add(folder);
         return list;
        }else if (folder.isDirectory()){
           addFileToList(folder.getPath(),folder,list);
        }else {
            log.error("不存在此文件");
            return null;
        }
        return list;
    }

    private static void addFileToList(String parent,File file,List<File> list){
        if (!parent.endsWith("/")){
            parent+="/";
        }
        File[] files = file.listFiles();
        for (File file1:files){
            if (file1.isDirectory()){
                String path = parent+file1.getName();
                addFileToList(path, file1, list);
            }else {
                list.add(file1);
            }
        }
    }

    public static void main(String[] args) {
//        String dest = "D:/5/";
////                createTestData();
////           toFtpOracleService.To221Ftp();
//        File file = new File("D:/allLog/");
//        CopyFolderUtil.copy(dest,file,true);
//        String copyFolderPath1 = "D:/allLog/";
//        File file1 = new File(copyFolderPath1);
//        List<File> list1 = CopyFolderUtil.copyFolderAllFile(file1);
//        System.out.println(list1.size());
//        System.out.println(list1);
//        String copyFolderPath = "D:/FTP/";
//        File file = new File(copyFolderPath);
//        List<File> list = CopyFolderUtil.copyFolderAllFile(file);
//        System.out.println(list.size());
//        System.out.println(list);
//        System.out.println(list.size()==0);
//        System.out.println(list==null);
//        System.out.println(CollectionUtils.isEmpty(list));
        File file = new File("D:/allLog/log/手提電腦(含臺式電腦主機)攜入攜出申請單new(1).doc");
        File file1 = new File("D:/allLog/asd/");
        file.renameTo(file1);



    }
}
